/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.controller;


import com.xlx.openLink.base.BaseDateMessage;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.conf.MysqlProperties;
import com.xlx.openLink.entity.pojo.Backup;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.service.BackService;
import com.xlx.openLink.vo.RequestDevtLst;
import com.xlx.openLink.vo.RequestSBackList;
import com.xlx.openLink.vo.RequestSystLst;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


@Controller
@RequestMapping("/back")
@Slf4j
public class BackController {

    @Autowired
    private BackService backService;

    @Autowired
    private MysqlProperties mysqlProperties;

    @PostMapping("/listByPage")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestBody RequestSBackList requestSBackList) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(backService.listByPage(requestSBackList));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


    @GetMapping("/del")
    @ResponseBody
    public BaseDateMessage del(@RequestParam(value = "pkGuid") String pkGuid) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(backService.delBackUp(pkGuid));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("操作成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("操作操作失败");
        }
        return baseDateMessage;
    }

    /**
     * 下载文件
     *
     * @param pkGuid
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/download")
    @ResponseBody
    public BaseDateMessage download(@RequestParam(value = "pkGuid") String pkGuid,
                                    HttpServletRequest request,
                                    HttpServletResponse response) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            Backup backup = backService.selectById(pkGuid);
            this.setResponseHeader(response, backup.getBakName());
            OutputStream os = response.getOutputStream();
            File file = new File(backup.getBakPath());
            InputStream inputStream = new FileInputStream(file);
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
            os.flush();
            os.close();
            inputStream.close();
        } catch (ThreadRunTimeExcption | IOException excption) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
        }
        return baseDateMessage;
    }

    /**
     * 上传文件
     *
     * @param file
     * @param name
     * @param id
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public BaseDateMessage upload(MultipartFile file,
                                  @RequestParam(value = "name") String name,
                                  @RequestParam(value = "id") String id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            backService.upload(name, id, file);
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("上传成功");
        } catch (ThreadRunTimeExcption | IOException e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
        }
        return baseDateMessage;
    }


    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
